﻿using Gfw.Infrastructure.Account;
using Gfw.Infrastructure.ExceptionImpl;
using Gfw.Infrastructure.Facilities;
using Gfw.Models.Account;
using Gfw.Repositories.Account;
using Gfw.Web.Models.Account;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gfw.BLL.Basic.Account
{
    /// <summary>
    /// 系统账户
    /// </summary>
    public class SystemAccountLogic
    {
        #region 账户信息

        /// <summary>
        /// 查询会员的钱
        /// </summary>
        /// <returns></returns>
        public static decimal QueryProfit()
        {
            ActCompanyAccountinformationRepository repository = new ActCompanyAccountinformationRepository();
            ActCompanyAccountinformation comAccount = repository.Unique();

            return comAccount.Profit;
        }

        /// <summary>
        /// 查询公司利润
        /// </summary>
        /// <returns></returns>
        public static decimal QueryBonus()
        {
            ActCompanyAccountinformationRepository repository = new ActCompanyAccountinformationRepository();
            ActCompanyAccountinformation comAccount = repository.Unique();

            return comAccount.Bonus;
        }

        #endregion

        #region 支出

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="selectTypeIndex">所选下拉</param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalSize"></param>
        /// <returns></returns>
        public static IList<OutlayModel> QueryOutlay(int selectTypeIndex, DateTime? start, DateTime? end, int pageIndex, int pageSize, out int totalSize)
        {
            int[] payTypes = null; // 表示查询全部
            switch (selectTypeIndex)
            {
                case (int)CompanyTradeTypeEnum.Pay_Bonus: // 分红支出
                    payTypes = new int[] { (int)CompanyTradeTypeEnum.Pay_Bonus };
                    break;

                case (int)CompanyTradeTypeEnum.Pay_Others: // 其它费用
                    payTypes = new int[] { (int)CompanyTradeTypeEnum.Pay_Others };
                    break;

                default:
                    payTypes = null;
                    break;
            }

            ActCompanyPayRepository payRep = new ActCompanyPayRepository();
            var payList = payRep.Query(payTypes, start, end, pageIndex, pageSize, out totalSize);
            var models = new List<OutlayModel>();
            foreach (var pay in payList)
            {
                OutlayModel model = pay.CopyTo<OutlayModel>();
                switch (pay.PayType)
                {
                    case (int)CompanyTradeTypeEnum.Pay_Bonus: // 分红支出
                        model.PayType = "分红支出";
                        break;

                    case (int)CompanyTradeTypeEnum.Pay_Others: // 其它费用
                        model.PayType = "其它费用";
                        break;

                    default:
                        model.PayType = "";
                        break;
                }
                models.Add(model);
            }
            return models;
        }


        /// <summary>
        /// 创建系统支出
        /// </summary>
        /// <param name="count">金额</param>
        /// <param name="type">类型</param>
        /// <param name="handlerID">经办人</param>
        /// <param name="remark">摘要</param>
        public static void CreateOutlay(decimal count, int type, string handlerID, string remark)
        {
            if (count <= 0)
                throw new GfwVerifyException("金额不正确！");

            if (string.IsNullOrEmpty(handlerID))
                throw new GfwVerifyException("无经办人！");

            decimal bonus = QueryBonus();
            if(count>bonus)
                throw new GfwVerifyException("账户余额不足！");

            SystemOutlayFacility facility = new SystemOutlayFacility(count, type, handlerID, remark);
            facility.Execute();
        }

        #endregion
    }
}
